package org.chen.cmd;

import lombok.extern.slf4j.Slf4j;
import org.chen.model.admin.SysUser;
import org.chen.service.impl.SysUserServiceImpl;
import org.chen.utils.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class AdminInitRunner implements CommandLineRunner {
    @Autowired
    private SysUserServiceImpl sysUserService;
    @Autowired
    private PasswordUtil passwordUtil;

    @Override
    public void run(String... args) {
        // 检查是否已存在管理员账号
        SysUser admin = sysUserService.selectByUsername("admin");
        if (admin == null) {
            // 创建管理员账号
            SysUser user = new SysUser();
            user.setUsername("admin");
            // 默认密码：admin123
            user.setPassword(passwordUtil.encode("admin123"));
            user.setNickname("管理员");
            user.setEmail("1905333495@qq.com");
            user.setStatus(1);
            sysUserService.save(user);
            log.info("初始化管理员账号成功");
        }else {
            log.info("已经存在管理员账号，不再执行初始化操作");
        }
    }
}
